/**
 * Created by midori on 2015/12/13.
 */
(function() {
    var module = angular.module('AppCheck.TestManage', [
        "ui.router",
        "restangular",
        "services.utils.RestAngularHandler",
        "services.Utils",
        "directives.Wizard"
    ]);

    module.controller('addTestCtrl', [
        "$scope",
        "$http",
        "RestAngularHandlerFactory",
        "Restangular",
        "$state",
        "utils",
        function($scope, $http, restAngularHandlerFactory, restangular, $state, utils){
            var init = function(){
                // store the flag variable indicating the status of the http call.
                $scope.rest = {};
                $scope.title = $state.current.data.displayName;
                $scope.targetApkPromise = restangular.all('target-apk');
                $scope.testApkPromise = restangular.all('test-code');

                $scope.testName = "";
                $scope.testCode = "";
                $scope.testCodes = [];
                $scope.testCodesID = {};
                $scope.targetApk = "";
                $scope.targetApks = [];
                $scope.targetApksID = {};
                $scope.wizardBuilderParameters = [
                    {
                        title: "Select your target app.",
                        fields:[
                            {
                                name:"Target Apk",
                                value:$scope.targetApk,
                                type:"select",
                                options: $scope.getTestApks(),
                                placeholder: "Choose a target app...",
                                hint: "Please choose the app you want to test"
                            }
                        ]
                    },{
                        title: "Select your test code.",
                        fields:[
                            {
                                name:"Test Code",
                                value:$scope.testCode,
                                type:"select",
                                options: $scope.getTestCodes(),
                                placeholder: "Choose a test code...",
                                hint: "Please choose the test code you want to run"
                            }
                        ]
                    },{
                        title: "Input Test Name.",
                        fields:[
                            {
                                name:"Test name",
                                type:"input",
                                placeholder: "Input test name...",
                                hint: "Please input test name"
                            }
                        ]
                    }
                ];
            };
            $scope.getTestApks = function() {
                $scope.targetApks = [];
                $scope.targetApksID = {};
                var getTargetAppRESTHandler = restAngularHandlerFactory.createAndStart($scope.rest);
                return $scope.targetApkPromise.getList().then(
                    getTargetAppRESTHandler.getSuccessCallback(
                        function (result) {
                            for(var i = 0; i < result.length; i++){
                                var option_name = i + ": " + result[i].name;
                                $scope.targetApks.unshift(option_name);
                                $scope.targetApksID[option_name] = result[i]._id;
                            }
                            return $scope.targetApks;
                        }
                    ),
                    getTargetAppRESTHandler.getErrorCallback(
                        //TODO: handle the error
                        function (errorResponse) {
                            console.error(errorResponse);
                        }
                    )
                );
            };

            $scope.getTestCodes = function(){
                $scope.testCodes = [];
                $scope.testCodesID = {};
                var getTestAppRESTHandler = restAngularHandlerFactory.createAndStart($scope.rest);
                return $scope.testApkPromise.getList().then(
                    getTestAppRESTHandler.getSuccessCallback(
                        function (result) {
                            for(var i = 0; i < result.length; i++){
                                var option_name = i + ": " + result[i].name + " - " + result[i].appName;
                                $scope.testCodes.unshift(option_name);
                                $scope.testCodesID[option_name] = result[i]._id;
                            }
                            return $scope.testCodes;
                        }
                    ),
                    getTestAppRESTHandler.getErrorCallback(
                        //TODO: handle the error
                        function (errorResponse) {
                            console.error(errorResponse);
                        }
                    )
                );
            };

            $scope.addNewTest = (function($scope){
                return function(targetApkName, testCodeName, name){
                    var reqBody = {
                        codeID : $scope.testCodesID[testCodeName],
                        targetApkID  : $scope.targetApksID[targetApkName],
                        name : name
                    };

                    console.log(reqBody);

                    var newTestRESTHandler = restAngularHandlerFactory.createAndStart($scope.rest);
                    restangular.all('test-plan').post(reqBody).then(newTestRESTHandler.getSuccessCallback(function(test){
                        window.location = "#/testing-dashboard";
                    }),newTestRESTHandler.getErrorCallback(function(result){
                        alert("failed");
                    }));
                };
            })($scope);

            init();
        }
    ]);

})(this);